package tio.study.helloworld.server;

import java.nio.ByteBuffer;

import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.GroupContext;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;

import tio.study.helloworld.common.AbsHandler;
import tio.study.helloworld.common.HelloPacket;

/**
 * 服务端handler处理类
 * 由于编解码的相关操作已经在common公共类中处理，所以只需进行消息处理即可。
 * @author Administrator
 *
 */
public class ServerHandler extends AbsHandler implements ServerAioHandler<Object,HelloPacket,Object>{
	/**
	 * 处理消息
	 */
	@Override
	public Object handler(HelloPacket packet, ChannelContext<Object, HelloPacket, Object> channelContext)
			throws Exception {
		byte[] body = packet.getBody();
		if(body != null){
			String str = new String(body,HelloPacket.CHARSET);
			System.out.println("收到消息，客户端的消息是："+ str);
			
			HelloPacket respPacket = new HelloPacket();
			String resp = "收到消息，你的消息是：" + str;
			respPacket.setBody(resp.getBytes(HelloPacket.CHARSET)); //组包
			
			Aio.send(channelContext,respPacket); //
		}
		return null;
	}
	
}
